package com.mz.ozy.activity.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.adapter.AddressAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.AddressBean;
import com.mz.ozy.bean.AddressListBean;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.AddressPresenter;
import com.mz.ozy.mvp.view.AddressContract;
import com.mz.ozy.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by administrator on 2018/11/21.
 * 收货地址列表
 */
public class AddressActivity extends BaseActivity<AddressContract.View, AddressPresenter> implements AddressContract.View {

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.ll_not_data)
    LinearLayout mLlNotData;

    private List<AddressBean> mList = new ArrayList<>();
    private AddressAdapter mAdapter;
    private int requestCode;
    private String mAid;
    private String mDaid;
    private boolean isDelete = false;
    private boolean isDeleteDefault = false;
    private AddressBean addressBean;
    private boolean isUpdate;

    /**
     * @param activity
     * @param requestCode 不为0，则返回地址给上一级
     */
    public static void go(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, AddressActivity.class);
        intent.putExtra("requestCode", requestCode);
        if (requestCode == 0) {
            activity.startActivity(intent);
        } else {//需返回结果给上一层
            activity.startActivityForResult(intent, requestCode);
        }
    }

    /**
     * @param activity
     * @param requestCode 不为0，则返回地址给上一级
     */
    public static void go(Activity activity, int requestCode, String aid, String defaultAddress) {
        Intent intent = new Intent(activity, AddressActivity.class);
        intent.putExtra("requestCode", requestCode);
        intent.putExtra("aid", aid);
        intent.putExtra("daid", defaultAddress);
        if (requestCode == 0) {
            activity.startActivity(intent);
        } else {//需返回结果给上一层
            activity.startActivityForResult(intent, requestCode);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText(R.string.str_receive_address);
        initView();
        showLoading();
    }

    private void initView() {
        mAid = getIntent().getStringExtra("aid");
        mDaid = getIntent().getStringExtra("daid");
        mAdapter = new AddressAdapter(mList);
        requestCode = getIntent().getIntExtra("requestCode", 0);
        if (requestCode != 0) {   //返回选中地址给上一层
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent();
                    intent.putExtra("data", mList.get(position));
                    setResult(RESULT_OK, intent);
                    finish();
                }
            });
        }
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_del) {
                    if (!TextUtils.isEmpty(mDaid) && mDaid.equals(mList.get(position).getId() + "")) {
                        isDeleteDefault = true;
                    } else if (!TextUtils.isEmpty(mAid) && mAid.equals(mList.get(position).getId() + "")) {
                        isDelete = true;
                    }
                    mPresenter.onDel(AddressActivity.this, mList.get(position).getId() + "");
                } else if (view.getId() == R.id.tv_default) {
                    addressBean = mList.get(position);
                    mPresenter.onDefault(AddressActivity.this,
                            mList.get(position).getId() + "");
                } else if (view.getId() == R.id.tv_edit) {
                    isUpdate = false;
                    if (!TextUtils.isEmpty(mAid) && mAid.equals(mList.get(position).getId() + "")) {
                        isUpdate = true;
                    }
                    AddressEditActivity.go(AddressActivity.this, 1, mList.get(position));
                }
            }
        });
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.onList(this);
    }

    @OnClick(R.id.bt_add)
    public void onViewClicked() {
        AddressEditActivity.go(this, 0, null);
    }

    @Override
    public void onListSuccess(AddressListBean bean) {
        mList.clear();
        mList.addAll(bean.getAddressList());
        if (isUpdate && mList != null) {
            for (AddressBean addressBean : mList) {
                if (!TextUtils.isEmpty(mAid) && mAid.equals(addressBean.getId() + "")) {
                    EventBus.getDefault().post(new EventBean(Constants.EventCode.EDIT_ADDRESS, addressBean));
                }
            }
        }

        if (mList.size() > 0) {
            mRecyclerView.setVisibility(View.VISIBLE);
            mLlNotData.setVisibility(View.GONE);
        } else {
            mRecyclerView.setVisibility(View.GONE);
            mLlNotData.setVisibility(View.VISIBLE);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onAddSuccess() {

    }

    @Override
    public void onUpdateSuccess() {
        //设置默认地址成功，重新请求数据
        mPresenter.onList(this);
    }

    @Override
    public void onDelSuccess() {
        ToastUtils.showToast("地址删除成功");
        if (isDeleteDefault) {
            EventBus.getDefault().post(new EventBean(Constants.EventCode.DELETE_DEFAULT_ADDRESS, null));
        } else if (isDelete) {
            EventBus.getDefault().post(new EventBean(Constants.EventCode.DELETE_ADDRESS, null));
        }
        mPresenter.onList(this);
    }

    @Override
    public void onDefaultSuccess() {
        addressBean.setIsdefault(1);
        EventBus.getDefault().post(new EventBean(Constants.EventCode.CHANGE_DEFAULT_ADDRESS, addressBean));
        mPresenter.onList(this);
    }
}
